package com.hiup.film.web.login;

import org.apache.commons.io.FileUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

@RestController
public class UploadControl {

    private String defalutDir = "./file/";

    @PostConstruct
    public void init(){
        File dir = new File(defalutDir);
        try {
            FileUtils.forceMkdir(dir);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "/testuploadimg",method = RequestMethod.POST)
    public String upload(@RequestParam("file")  MultipartFile multipartFile, HttpServletRequest httpServletRequest) throws IOException {
        String fileName = new String(multipartFile.getOriginalFilename().getBytes("gBK"),"UTF-8");;
        //new String(multipartFile.getOriginalFilename().getBytes("ISO8859-1"),"UTF-8");
        //FileUtils.forceMkdir();
        File file = new File(defalutDir + fileName);
        FileUtils.copyInputStreamToFile(multipartFile.getInputStream(),file);
        return "hello";
    }

}
